package com.gdth.bank.model;

import java.util.Date;
import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.database.bean.TAppBfrjCpxq;
import com.gdth.database.bean.TAppBfrjCpxqDao;

import org.greenrobot.greendao.query.QueryBuilder;


public class TAppBfrjJtcyCpxqModel {
	public Observable<List<TAppBfrjCpxq>> savejtcyCpxq(final List<TAppBfrjCpxq> mTAppBfrjCpxqList){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjCpxq>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjCpxq>> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjCpxqDao dao = BankApplication.getDaoSession().getTAppBfrjCpxqDao();
			  if(mTAppBfrjCpxqList.size()>0){
				  for(int i=0;i<mTAppBfrjCpxqList.size();i++){
					  TAppBfrjCpxq mTAppBfrjCpxq= mTAppBfrjCpxqList.get(i);
					  dao.insert(mTAppBfrjCpxq);
				  }
				  CJkhxxModel.updateCjkhJbxxZt(mTAppBfrjCpxqList.get(0).getJcuuid());
			  }
			  if(mTAppBfrjCpxqList==null){
					arg0.onError(null);
				}else{
					arg0.onNext(mTAppBfrjCpxqList);
					arg0.onCompleted();
				}
		
			
			}
		});
	}
	public Observable<List<TAppBfrjCpxq>> getjtcyxxCpxq(final String jtuuid){
		return Observable.create(new Observable.OnSubscribe<List<TAppBfrjCpxq>>() {

			@Override
			public void call(Subscriber<? super List<TAppBfrjCpxq>> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjCpxqDao dao = BankApplication.getDaoSession().getTAppBfrjCpxqDao();
				QueryBuilder<TAppBfrjCpxq> qb = dao.queryBuilder();
				qb.where(TAppBfrjCpxqDao.Properties.Jtuuid.eq(jtuuid));
				qb.where(TAppBfrjCpxqDao.Properties.Yxdjzt.eq(1));
				List<TAppBfrjCpxq> mList = qb.list();
				
					arg0.onNext(mList);
					arg0.onCompleted();
				
			}
		});
	}
	public Observable<TAppBfrjCpxq> getjtcyxxCpxqId(final Long id){
		return Observable.create(new Observable.OnSubscribe<TAppBfrjCpxq>() {

			@Override
			public void call(Subscriber<? super TAppBfrjCpxq> arg0) {
				// TODO Auto-generated method stub
				TAppBfrjCpxqDao dao = BankApplication.getDaoSession().getTAppBfrjCpxqDao();
				QueryBuilder<TAppBfrjCpxq> qb = dao.queryBuilder();
				qb.where(TAppBfrjCpxqDao.Properties.Id.eq(id));
				List<TAppBfrjCpxq> mList = qb.list();
				TAppBfrjCpxq mTAppBfrjCpxq=	mList.get(0);
				mTAppBfrjCpxq.setYxdjzt(0);
				mTAppBfrjCpxq.setSjzt(0);
				mTAppBfrjCpxq.setXgsj(new Date());
				dao.update(mTAppBfrjCpxq);
				CJkhxxModel.updateCjkhJbxxZt(mTAppBfrjCpxq.getJcuuid());
				if(mList==null){
					arg0.onError(null);
				}else{
					
					arg0.onNext(mTAppBfrjCpxq);
					arg0.onCompleted();
				}
				
			}
		});
	}
}
